package com.demo.accessibility.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewConfiguration
import android.view.WindowManager
import android.widget.FrameLayout
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ScreenUtils
import kotlin.math.abs

class FloatWindowView : FrameLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes)

    private val mTouchSlop by lazy { ViewConfiguration.get(context).scaledTouchSlop }

    private val mStatusBarHeight by lazy { BarUtils.getStatusBarHeight() }
    private var mTouchDownX = 0f
    private var mTouchDownY = 0f
    private var mLastX = 0f
    private var mLastY = 0f
    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mTouchDownX = event.rawX
                mTouchDownY = event.rawY
            }
            MotionEvent.ACTION_MOVE -> {
                mLastX = event.rawX
                mLastY = event.rawY
                if (abs(mTouchDownX - event.rawX) > mTouchSlop || abs(mTouchDownY - event.rawY) > mTouchSlop) {
                    return true
                }
            }
            MotionEvent.ACTION_UP,
            MotionEvent.ACTION_CANCEL -> {

            }
        }
        return super.onInterceptTouchEvent(event)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {

            }
            MotionEvent.ACTION_MOVE -> {
                var dx = mLastX - event.rawX
                var dy = event.rawY - mLastY
                mLayoutParams?.apply {
                    x += dx.toInt()
                    y += dy.toInt()
                    if (x < 0) {
                        x = 0
                    } else if (x > ScreenUtils.getScreenWidth() - getWidth()) {
                        x = ScreenUtils.getScreenWidth() - getWidth()
                    }
                    if (y > (ScreenUtils.getScreenHeight() - mStatusBarHeight - getHeight()) / 2) {
                        y = (ScreenUtils.getScreenHeight() - mStatusBarHeight - getHeight()) / 2
                    } else if (y < -(ScreenUtils.getScreenHeight() - mStatusBarHeight - getHeight()) / 2) {
                        y = -(ScreenUtils.getScreenHeight() - mStatusBarHeight - getHeight()) / 2
                    }
                }
                mWindowManager?.updateViewLayout(this, mLayoutParams)
            }
            MotionEvent.ACTION_UP,
            MotionEvent.ACTION_CANCEL -> {

            }
        }
        mLastX = event.rawX
        mLastY = event.rawY
        return true
    }

    private var mWindowManager: WindowManager? = null

    private var mLayoutParams: WindowManager.LayoutParams? = null
    fun attachWindowManager(
        windowManager: WindowManager?,
        layoutParams: WindowManager.LayoutParams?
    ) {
        mWindowManager = windowManager
        mLayoutParams = layoutParams
    }
}